#include "BufferObject.h"

GLuint      BufferObject::s_WritingVBO;
GLuint      BufferObject::s_DisplayingVBO;

GLuint      BufferObject::s_WritingIBO;
GLuint      BufferObject::s_DisplayingIBO;

BufferObject::BufferObject(){}

void BufferObject::InitBuffers()
{
    InitVBO();
    InitIBO();
}

void BufferObject::InitVBO()
{
    glGenBuffers(1,&(s_WritingVBO));
    glGenBuffers(1,&(s_DisplayingVBO));
}

void BufferObject::InitIBO()
{
    glGenBuffers(1, &s_WritingIBO);
    glGenBuffers(1, &s_DisplayingIBO);
}

void BufferObject::BindVBO()
{
    glBindBuffer(GL_ARRAY_BUFFER, s_DisplayingVBO);
}

void BufferObject::BindIBO()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_DisplayingIBO);
}

void BufferObject::FeedBuffers()
{
    FeedVBO();
    FeedIBO();
    Swap();
}

void BufferObject::FeedVBO()
{
    glBindBuffer(GL_ARRAY_BUFFER, s_WritingVBO);
    glBufferData(GL_ARRAY_BUFFER,Vertex::Vertices.size()*4*4*sizeof(float),Vertex::Array(),GL_STREAM_DRAW);
}

void BufferObject::FeedIBO()
{
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, s_WritingIBO);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,Face::Faces.size()*sizeof(unsigned short)*3,Face::Array(),GL_STREAM_DRAW);
}

void BufferObject::Swap()
{
    GLuint tempValueVBO = s_WritingVBO;
    GLuint tempValueIBO = s_WritingIBO;

    s_WritingVBO        =   s_DisplayingVBO;
    s_DisplayingVBO     =   tempValueVBO;

    s_WritingIBO        =   s_DisplayingIBO;
    s_DisplayingIBO     =   tempValueIBO;
}
